package com.yunkaweilai.android.view.popup;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.operation.consumption.guadan.GuadanLableActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.DialogMemberPayInterface;
import com.yunkaweilai.android.model.SimpleModel;
import com.yunkaweilai.android.model.guadan.LableModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;
import com.yunkaweilai.android.view.dialog.guadan.GuadanSuccessDialog;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;

import razerdp.basepopup.BasePopupWindow;

/**
 * Created by Administrator on 2017/7/17.
 */

public class ShopGuadanPopup extends BasePopupWindow {

    private View popupView;
    private Activity mContext;
    private LayoutInflater mInflater;
    private TagFlowLayout idTagFlow;
    private TagAdapter<LableModel.DataBean.ListBean> tagAdapter;
    private ArrayList<LableModel.DataBean.ListBean>list = new ArrayList<>();
    private EditText idEdtGuadan;
    private String remark;
    private String type;
    private String selected_goods_str;
    private String gid;
    private DialogMemberPayInterface dialogMemberPayInterface;
    /**
     *
     * @param context
     * @param selected_goods_str
     * @param remark
     * @param type    0第一次挂单的备注。 1挂单修改备注
     */
    public ShopGuadanPopup(Activity context,String selected_goods_str,String remark,String type,String gid) {
        super(context);
        this.mContext = context;
        this.selected_goods_str = selected_goods_str;
        this.remark = remark;
        this.type = type;
        this.gid = gid;
        bindEvent();
    }

    public void setmInflater(DialogMemberPayInterface dialogMemberPayInterface){
        this.dialogMemberPayInterface = dialogMemberPayInterface;
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateAnimation(250 * 2, 0, 300);
    }

    @Override
    public View getClickToDismissView() {
        return popupView.findViewById(R.id.id_img_close);
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.popup_shop_guadan, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return popupView.findViewById(R.id.id_rlayout_bottom);
    }

    private void bindEvent() {
        mInflater = LayoutInflater.from(mContext);
        initHttp();
        if (popupView != null) {
            idEdtGuadan = ((EditText) popupView.findViewById(R.id.id_edt_guadan));
            idEdtGuadan.setText(remark + "");
            idEdtGuadan.setSelection(idEdtGuadan.length());
            idTagFlow = ((TagFlowLayout) popupView.findViewById(R.id.id_tag_flow_layout));
            initAdapter();
            initClick();

        }
    }

    private void initClick() {
        popupView.findViewById(R.id.id_tv_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (type){
                    case "0":
                        initGuadan();
                        break;
                    case "1":
                        updateRemark();
                        break;
                }
            }
        });
    }

    private void initAdapter() {
        tagAdapter = new TagAdapter<LableModel.DataBean.ListBean>(list) {
            @Override
            public View getView(FlowLayout parent, int position, LableModel.DataBean.ListBean lableModel) {
                TextView tv = (TextView) mInflater.inflate(R.layout.item_flow_tv2,
                        idTagFlow, false);
                tv.setText(lableModel.getTitle());
                return tv;
            }
        };
        idTagFlow.setAdapter(tagAdapter);
        idTagFlow.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()
        {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent)
            {
                if (position == list.size() -1){
                    mContext.startActivity(new Intent(mContext, GuadanLableActivity.class));
                    dismiss();
                }else{
                    idEdtGuadan.setText(idEdtGuadan.getText().toString() + list.get(position).getTitle());
                    idEdtGuadan.setSelection(idEdtGuadan.length());
                }
                return true;
            }
        });
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.GUADAN_LABLE_LIST)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mContext, json);
                        if (checkCode){
                            Gson gson = new Gson();
                            LableModel lableModel = gson.fromJson(json, LableModel.class);
                            list.clear();
                            list.addAll(lableModel.getData().getList());
                            LableModel.DataBean.ListBean models = new LableModel.DataBean.ListBean();
                            models.setTitle("+");
                            list.add(models);
                            tagAdapter.notifyDataChanged();

                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }


    private void initGuadan(){
        HttpRequest.postUrl(Api.GUADAN_ORDER_FIRST)
                .addParams("SelectedGoodsStr",selected_goods_str)
                .addParams("remark",idEdtGuadan.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mContext, json);
                        if (checkCode){
                            Gson gson = new Gson();
                            SimpleModel simpleModel = gson.fromJson(json, SimpleModel.class);
                            GuadanSuccessDialog dialog = new GuadanSuccessDialog(mContext,simpleModel.getData().getGid());
                            dialog.setCanceledOnTouchOutside(false);
                            dialog.show();
                            dismiss();
                        }
                    }
                    @Override
                    public void onError(Exception e) {

                    }
                });

    }


    private void updateRemark(){
        HttpRequest.postUrl(Api.UPDATE_GUADAN_REMARK)
                .addParams("gid",gid)
                .addParams("remark",idEdtGuadan.getText().toString()+"")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mContext, json);
                        if (checkCode){
                            MyToast.showShortToast(mContext,"修改成功");
                            if (dialogMemberPayInterface!=null){
                                dialogMemberPayInterface.passPay(idEdtGuadan.getText().toString() +"");
                            }
                            dismiss();
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }


}